#include "server.h"

int fd_tty,fd_led,flag=0;
int ret = -1;
pthread_mutex_t mutex1,mutex2;
int sockfd_tty,acceptfd_tty;
char buf=0x80;
char led_buf[4]="0";
struct sockaddr_in serveraddr,clientaddr;
socklen_t clientlen;
char sql[128];
int main(int argc, char *argv[])
{
    pthread_t p_zig,p_qt,p_sqlite;
    sockfd_tty = socket(AF_INET,SOCK_STREAM,0); 
	serveraddr.sin_family=AF_INET;
	serveraddr.sin_port=htons(atoi(argv[2]));
	serveraddr.sin_addr.s_addr=inet_addr((argv[1]));
	socklen_t serverlen = sizeof(serveraddr);
	clientlen = sizeof(clientaddr);
	if(bind(sockfd_tty,(struct sockaddr*)&serveraddr,serverlen)<0)
	{
		perror("bind err:");
		return -1;
	}
	printf("bind is ok!\n");
	listen(sockfd_tty,8);
	printf("listen is start\n");
    pthread_mutex_init(&mutex1, NULL);
    pthread_mutex_init(&mutex2, NULL);
    pthread_mutex_lock(&mutex2);
    fd_tty = open("/dev/ttyUSB0",O_RDWR);
    if(fd_tty<0)
    {
        perror("open ttyUSB0 failed");
        return -1;
    }
    fd_led = open("/dev/a53_led_drive",O_RDWR);
    if(fd_led < 0){
         perror("open led_drive failed");
        return -1;
    }
    serial_init(fd_tty);
    pthread_create(&p_zig,NULL,p_thread_zig,NULL);
    pthread_create(&p_qt,NULL,p_thread_qt,NULL);
    pthread_join(p_zig,NULL);
    pthread_join(p_qt,NULL);
    return 0;
}

void *p_thread_zig(void *arg)
{
    info info_zig;
    char zig_recv_buf[2048];
     while(1)
    {
        read(fd_tty,&info_zig,sizeof(info_zig));
        pthread_mutex_lock(&mutex1);
        // printf("zig.flag = %d\n",flag);
        // printf("head  = %c%c%c\n",info_zig.head[0],info_zig.head[1],info_zig.head[2]);
        printf("x轴= %-10d",info_zig.x);
        printf("y轴= %-10d\t",info_zig.y);
        printf("z轴= %d\n",info_zig.z);
        printf("光照强度 = %d\n",info_zig.ill);
        printf("仓库温度 = %d.%d\n",info_zig.temp[0],info_zig.temp[1]);//
        printf("仓库湿度 = %d.%d\n",info_zig.hum[0],info_zig.hum[1]);//
        if(flag == 0)
        {
            pthread_mutex_unlock(&mutex1);
        }
        sprintf(zig_recv_buf,"二号仓库\t          %d.%d\t%d.%d         \t   %d               %d \t         %d",\
        info_zig.temp[0],info_zig.temp[1],info_zig.hum[0],info_zig.hum[1],\
        info_zig.x,info_zig.y,info_zig.z);
        if(acceptfd_tty > 0)
              write(acceptfd_tty,zig_recv_buf,sizeof(zig_recv_buf));
        pthread_mutex_unlock(&mutex2);
        sleep(1);
    }
}
void *p_thread_qt(void *arg)
{
    acceptfd_tty= accept(sockfd_tty,(struct sockaddr*)&clientaddr,&clientlen);
	printf("client connected\n");
    ret = 1;
    while(1)
    {
        memset(led_buf,0,sizeof(led_buf));
        ret = recv(acceptfd_tty,&buf,sizeof(buf),0);
        if(ret == 0)
        {
                printf("client quit\n");
                flag = 0;
                pthread_mutex_unlock(&mutex1);
                pthread_mutex_lock(&mutex2);
                close(acceptfd_tty);
                acceptfd_tty= accept(sockfd_tty,(struct sockaddr*)&clientaddr,&clientlen);
        }
        printf("qt.flag = %d,acceptfd = %d,buf = %c\n",flag,acceptfd_tty,buf);
       if(ret > 0)
       {
           flag = 1;
           pthread_mutex_lock(&mutex2);
       }
         flag = 0;
        pthread_mutex_unlock(&mutex1);
         switch (buf){
             case '8':
                    buf = 0x80;//close fan
                    break;
             case '5':
                    buf = 0x81;//fan level1
                    break;
            case '6':
                    buf = 0x82;//fan level2
                    break;
            case '7':
                    buf = 0x83;//fan level3
                    break;
            case '4':
                    buf = 0x90;//close M0 beep
                    break;
            case '3':
                    buf = 0x91;//open M0 beep
                    break;
            // case '7':
            //         buf = 0x92;//close auto alarm
            //         break;
            // case '8':
            //         buf = 0x93;//open auto alarm
            //         break;
            case '2':
                    buf = 0xA0;//close led
                    break;
            case '1':
                    buf = 0xA1;//open led
                    break;         
            case '9':
                    led_buf[0] = 1;
                    led_buf[3] = 0;
                    break;
            case 'A':
                    led_buf[0] = 0;
                    break;
            case 'B':
                    led_buf[0] = 0;
                    led_buf[3] = 1;
                    break;
            case 'C':
                    led_buf[3] = 0;
                    break;
         }
        write(fd_tty,&buf,sizeof(buf));
        write(fd_led,led_buf,sizeof(led_buf));
        
    }
}

void serial_init(int fd_tty) {
    struct termios options;
    tcgetattr(fd_tty, &options);
    options.c_cflag |= ( CLOCAL | CREAD );
    options.c_cflag &= ~CSIZE;
    options.c_cflag &= ~CRTSCTS;
    options.c_cflag |=CS8;
    options.c_cflag &= ~CSTOPB; 
    options.c_iflag |=IGNPAR;
    options.c_iflag &= ~(ICRNL | IXON);
    options.c_oflag  = 0;
    options.c_lflag  = 0;
     tcflush(fd_tty,TCIFLUSH); 
    cfsetispeed(&options,B115200);
    cfsetospeed(&options,B115200);
    tcsetattr(fd_tty,TCSANOW, &options);

}

